$(function (){
    $('#fill').on('click',function (e){
        e.preventDefault();
        $.ajax({
            type    : 'POST',
            url     : 'http://localhost:8080/newOrder',
            data    : {
                kind    : 'getAccount',
            },
            success : function (data){
                // $('#order.billToFirstName').val(info[1]);
                $('#creditCard').val(data.creditCard);
                $('#expiryDate').val(data.expiryDate);
                $('#billToFirstName').val(data.billToFirstName);
                $('#billToLastName').val(data.billToLastName);
                $('#billAddress1').val(data.billAddress1);
                $('#billAddress2').val(data.billAddress2);
                $('#billCity').val(data.billCity);
                $('#billState').val(data.billState);
                $('#billZip').val(data.billZip);
                $('#billCountry').val(data.billCountry);
            },
            error   : function (errorMsg){
                console.log(errorMsg);
            }

        });
    })
    $('#submit').on('click',function (e){
        e.preventDefault();
        $.ajax({
            type    : 'POST',
            url     : 'http://localhost:8080/newOrder',
            data    : {
                kind            : '',
                reditCard       : $('#reditCard').val(),
                expiryDate      : $('#expiryDate').val(),
                billToFirstName : $('#billToFirstName').val(),
                billToLastName  : $('#billToLastName').val(),
                billAddress1    : $('#billAddress1').val(),
                billAddress2    : $('#billAddress2').val(),
                billCity        : $('#billCity').val(),
                billState       : $('#billState').val(),
                billZip         : $('#billZip').val(),
                billCountry     : $('#billCountry').val()
            },
            success : function (data){
                if(data.indexOf("address") !== -1){
                    window.location.href='http://localhost:8080/' + data.substring(data.indexOf(":") + 1, data.length);
                }else{
                    $('#error-msg').text(data);
                    console.log('register ='+data);
                }
            },
            error   : function (errorMsg){
                console.log(errorMsg);
            }
        })
    })
    $('#creditCard').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Card Number cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#expiryDate').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Expiry Date cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#billToFirstName').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Firstname cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#billToLastName').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Lastname cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })
    $('#billAddress1').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Address1 cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#billAddress2').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Address2 cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#billCity').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The City cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#billState').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The State cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#billZip').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Zip cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })

    $('#billCountry').on('blur',function (){
        var info = $(this).val();
        if (info === ''){
            $('#error-msg').text('The Country cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    })
})